/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package py.una.pol.pweb.compraventa;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Serializable;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;

/**
 *
 * @author claudia
 */
@ManagedBean( name = "fileUploadController")
@SessionScoped
public class FileUploadController implements Serializable{

    private static final long serialVersionUID = 5088570493869770149L;

    public void handleFileUpload(FileUploadEvent event) {
        UploadedFile file = event.getFile();
        String linea;
        String [] token;
        InputStreamReader isr = null;
        BufferedReader buffer = null;
        try {
            isr = new InputStreamReader(file.getInputstream());
            buffer = new BufferedReader(isr);
            //Leer el la informacion del archivo linea por linea
           while ((linea = buffer.readLine()) != null) {
                System.out.println(linea);
                PagoMasivo pagoMasivo = new PagoMasivo();
                pagoMasivo.insertarPago(linea);

            }
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Exito!","El archivo "+ event.getFile().getFileName() + " fue cargado de manera exitosa!!");
            FacesContext.getCurrentInstance().addMessage(null, msg);
        } catch (IOException e) {
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Ocurrió un error al cargar el archivo ", event.getFile().getFileName());
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }

    }
}
